package com.gyc.datastructure.stack;

import java.util.Arrays;

/**
 * @author gyc
 * @date 2017/12/26
 * 数组实现栈
 */
public class MyStack<T> {
    private Object[] stack;
    int size;
    public MyStack(){
        stack=new Object[10];
    }
    public boolean isenpty(){
        return size==0;
    }

    public T peek(){
        if(isenpty()){
            return null;
        }
        return (T) stack[size-1];
    }

    public T pop(){
        T t=peek();
        stack[size-1]=null;
        size--;
        return t;
    }

    public T push(T t){
        ensureCapacity(size+1);
        stack[size++]=t;
        return t;
    }

    public void ensureCapacity(int size) {
        int len=stack.length;
        if(size>len){
            int newlen=10;
            stack= Arrays.copyOf(stack,newlen);
        }
    }

    public static void main(String[] args){
        MyStack<Integer> myStack=new MyStack<>();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
        System.out.println("栈顶元素："+myStack.pop());
        System.out.println("栈顶元素："+myStack.pop());
    }
}
